package dao;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DaoUtil {

public Connection cx = null;

    public DaoUtil(){
    }

    public Connection getConnection() throws ClassNotFoundException, SQLException{
        if(cx == null){
            String url = "jdbc:mysql://localhost/prova";
            String login = "root";
            String senha = "";
            Class.forName("com.mysql.jdbc.Driver");
            cx = DriverManager.getConnection(url, login, senha);
        }
        return cx;
    }

    public Statement getStatement() throws ClassNotFoundException, SQLException{
        return (Statement) getConnection().createStatement();
    }

    public PreparedStatement getPreparedStatement(String sql) throws ClassNotFoundException, SQLException{
        return getConnection().prepareStatement(sql);
    }

    public void fechar() throws SQLException{
        if(cx != null){
            cx.close();
        }
    }
}
